<template>
	<view class="order-list">
		<view class='list-box' :style="'height:'+( windowHeight)  + 'px'">
			<scroll-view :style="'height: '+(windowHeight-80) + 'px;'" scroll-y="true" class="scroll-Y">
				<view style=" height: 100%;">
					<view>
						<block v-for="(item1, index1) in pfMallOrderProductsItemList" :key="index1">
							<view class="list-item" style="margin-top: 28rpx;">
								<view class="list-cell">
									<image class="img" mode="aspectFill"
										:src="item1.imgUrl||'https://dnsverify.pfmoto.com/appstatic/img/my/appoint_img_small.png'" />
									<view class="list-content_text" style="width: 426rpx;">
										<view class="layout-1" style="justify-content: space-between;">
											<text class="text-5" style="width: 300rpx;text-align: left;">{{item1.productName}}</text>
										</view>
										<view class="layout-1" style="margin-top: 8px;">
											<text class="text-1">{{item1.attValues}}</text>
										</view>

										<view class="layout-1" style="justify-content: space-between;margin-top: 8px; ">
											<text class="text-5">商品评价</text>
											<u-rate activeColor="#EA5404" class="star" size="20" activeIcon="star-fill"
												:count="5" v-model="start">
											</u-rate>



										</view>
									</view>
								</view>
								<textarea class="uni-textarea" type="text" value='cancelResonText'
									v-model='cancelResonText' data-type='cancelResonText' maxlength="150" ref="textarea"
									placeholder="请输入评价内容" placeholder-style="color:#ADB0AE" auto-height />

								<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple
									:maxCount="5" style='margin-left: 32rpx;margin-bottom: 32rpx;margin-top: 32rpx;'>
								</u-upload>

							</view>
						</block>
					</view>
				</view>
			</scroll-view>

			<view class="cancelBtns">
				<text class="cancelBtn" @click="productCommentBtn">确定</text>
			</view>
		</view>
	</view>
</template>
<script>
	const app = getApp();
	const util = require('@/utils/util.js');
	const network = require('@/manager/network-manager.js');
	export default {
		data() {
			return {
				windowHeight: app.globalData.windowHeight - app.globalData.navigationBarHeight,
				navigationBarHeight: app.globalData.navigationBarHeight,
				contentHeight: app.globalData.windowHeight - 100,
				pfMallOrderProductsItemList: [],
				start: 0,

				cancelResonText: '',
				fileList: [],

			}
		},
		onLoad: function(options) {
			this.pfMallOrderProductsItemList = JSON.parse(options.pfMallOrderProductsItemListJson)

		},


		methods: {
			//评价确定，判断数据
			productCommentBtn: function() {
				if (this.start == 0) {
					uni.showToast({
						title: '请给商品打分',
						duration: 3000,
						icon: 'none'
					})
				} else if (this.cancelResonText == '') {
					uni.showToast({
						title: '请输入评价内容',
						duration: 3000,
						icon: 'none'
					})
				} else {
					this.addProductComment()
				}
			},
			//新建商品评价(APP)
			addProductComment: function() {
				var that = this
				var commentList = []
				var cmomentImgUrl = ''
				for (var i = 0; i < that.fileList.length; i++) {
					if (i == 0) {
						cmomentImgUrl = that.fileList[i].url
					} else {
						cmomentImgUrl = cmomentImgUrl + ',' + that.fileList[i].url
					}
				}
				var item = {
					cmomentImgUrl: cmomentImgUrl,
					context: that.cancelResonText,
					productId: that.pfMallOrderProductsItemList[0].productId,
					orderProductItemId: that.pfMallOrderProductsItemList[0].id,
					productOrderNo: that.pfMallOrderProductsItemList[0].orderNo,
					productType: that.pfMallOrderProductsItemList[0].goodsType,
					start: that.start
				}

				commentList.push(item)

				var url = app.globalData.config.interfaces.URL_ACTIVITY_PRODUCTCOMMENT_ADDPRODUCTCOMMENT
				var params = {
					commentList: commentList,
					userId: app.getUserId()
				};
				network.post(url, params, 'json', null, true).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none',
							success: () => {
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 2000)
							}
						})

					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
				});
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {

				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				var that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: app.globalData.config.BASE_URL + app.globalData.config.interfaces
							.URL_ALIYUN_OSS_UPLOAD, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							folder: 'community'
						},
						fileType: "image", //钉钉必填
						header: {
							"token": "Bearer " + app.globalData.token,
							"Authorization": "Bearer " + app.globalData.token,
						},
						success: (uploadFileRes) => {
							setTimeout(() => {
								resolve(JSON.parse(uploadFileRes.data).data)
							}, 1000)

							console.log(JSON.stringify(uploadFileRes))
							//小程序原生请求要先处理statusCode
							if (uploadFileRes.statusCode == 200) {
								var uploadFileData = JSON.parse(uploadFileRes.data);
								console.log("上传成功" + uploadFileRes.data);
								if (uploadFileData.code == 200) {
									that.picURL = uploadFileData.data
								} else {
									dialog.toast("上传失败" + uploadFileData.message)
								}
							} else {
								dialog.toast("上传失败，请重试")
								uploadTask.abort()
							}
						}
					});
				})
			},











		}
	}
</script>
<style>
	@import "./mallDetail.css";

	.empty {
		height: 100%;
		width: 100%;

	}

	.empty .img-box {
		width: 388rpx;
		position: relative;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}

	.empty .img-box image {
		width: 100%;
	}

	.empty .text {
		text-align: center;
		font-size: 28rpx;
		line-height: 40rpx;
		color: #ADB0AE;
	}

	.u-nav-slot {
		display: flex;
		flex-direction: row;
	}
</style>
